//
//  WBHomeViewController.swift
//  ZWWEIBO
//
//  Created by xzw on 2017/11/21.
//  Copyright © 2017年 xzw. All rights reserved.
//

import UIKit

//原创微博可重用cell id
private let originalCellId = "originalCellId"

//被转发微博的可重用cell id
private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {

    //视图模型
    private lazy var viewModel : WBStatusListViewModel = WBStatusListViewModel()
    
    //加载数据
    override func loadData() {
        
        //加载网络数据
        print("上拉加载数据最后一条 \(viewModel.statusList.last?.status.text ?? "")")
        viewModel.loadStatus(isPullup:self.isPullup) { (issuccess,hasMorePullup) in
            
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            self.isPullup = false //上拉结束一次
            if hasMorePullup
            {
               self.tableView?.reloadData()
            }
        }
    }
    
    //显示好友
    @objc private func showFriends(){
        print(#function)
       
    }
}
 //MARK: - 表格数据源方法
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //0.取出视图模型，根据视图模型判断可重用cell
        let vm = viewModel.statusList[indexPath.row]
        
        let cellID = (vm.status.retweeted_status != nil) ? retweetedCellId : originalCellId
        
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        
        //2.赋值
        cell.statusViewModel = vm
        
        //返回cell
        return cell
    }

}


extension WBHomeViewController{
   
    override func setupTableView() {
        super.setupTableView()
        
        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fonsize: 16, target: self, action: #selector(showFriends))
        
        //注册cell
        tableView?.register(UINib(nibName: "WBStatusNomalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        
         tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        
        //设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        //取消分割线
        tableView?.separatorStyle = .none
        
        //设置导航栏标题视图
        setupNavTitleView()
    }
   
    //设置导航栏标题视图
    private func setupNavTitleView(){
    
      let username = WBNetworkManager.sharedInstance.userModel.screen_name ?? ""
      let btn = WBTitleBtn(title:username )
      btn.addTarget(self, action: #selector(titleViewClickBtn), for: .touchUpInside)
      navItem.titleView = btn
    }
    
    @objc private func titleViewClickBtn(btn : UIButton){
        btn.isSelected = !btn.isSelected
    }
    
}
